package com.kai.service.ipml;

import com.github.pagehelper.PageHelper;
import com.kai.dto.FinanceWithdrawResponeDto;
import com.kai.dto.UserWithdrawResponeDto;
import com.kai.dto.WalletBlockHeightResponeDto;
import com.kai.entity.*;
import com.kai.mapper.second.*;
import com.kai.mapper.third.WalletBlockHeightMapper;
import com.kai.mapper.third.WalletFinanceMapper;
import com.kai.service.UsdtServie;
import com.kai.service.WalletService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by kai on 18-8-1.
 */
@Service("WalletService")
public class WalletServiceImpl implements WalletService {

    private static Logger log = LoggerFactory.getLogger(WalletServiceImpl.class);
    private final static String TX_TYPE_ONE="1";
    private final static String TX_TYPE_TOW="2";
    private final static String TX_TYPE_SIX="6";
    @Resource
    private WalletPlatformEntryMapper walletPlatformEntryMapper;
    @Resource
    private WalletPlatformOutMapper walletPlatformOutMapper;
    @Resource
    private WalletHotMapper walletHotMapper;
    @Resource
    private WalletHotEntryMapper walletHotEntryMapper;
    @Resource
    private WalletColdMapper walletColdMapper;
    @Resource
    private WalletPlatformMapper walletPlatformMapper;

    @Resource
    private WalletBlockHeightMapper walletBlockHeightMapper;
    @Resource
    private WalletFinanceMapper walletFinanceMapper;

    @Resource
    private UsdtServie usdtServie;


    @Override
    public Map<String, Object> getWalletPlatformData(WalletPlatformEntry w) {
        Map<String, Object> tableData = new HashMap<>();
        try {


            PageHelper.startPage(w.getPageNum(), w.getPageSize());
            List<WalletPlatformEntry> userList = walletPlatformEntryMapper.selectWalletPlatformEntry(w);
            int count = walletPlatformEntryMapper.getCount(w);
            tableData.put("list", userList);
            tableData.put("count", count);

            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();

        }

        return tableData;
    }

    @Override
    public Map<String, Object> getWalleWithDrawData(WalletPlatformOut w) {
        Map<String, Object> tableData = new HashMap<>();
        try {
            PageHelper.startPage(w.getPageNum(), w.getPageSize());
            List<WalletPlatformOut> userList = walletPlatformOutMapper.selectWalletColdEntry(w);
            int count = walletPlatformOutMapper.getCount(w);
            for (WalletPlatformOut  wpf:userList) {
                if(wpf.getMqStatus() !=null && wpf.getMqStatus().equals("1")){
                    wpf.setMqStatus("已发送");
                }else{
                    wpf.setMqStatus("未发送");
                }
            }
            tableData.put("list", userList);
            tableData.put("count", count);

            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();

        }

        return tableData;
    }

    @Override
    public Map<String, Object> getWalletHotData(WalletHot w,Integer pageNum,Integer pageSize) {
        Map<String, Object> tableData = new HashMap<>();
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<WalletHot> userList = walletHotMapper.selectWalletHot(w);
            int count = walletHotMapper.getCount(w);
            tableData.put("list", userList);
            tableData.put("count", count);

            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();

        }

        return tableData;
    }

    @Override
    public Map<String, Object> getWalletColdData(WalletCold w) {

        Map<String, Object> tableData = new HashMap<>();
        try {
            PageHelper.startPage(w.getPageNum(), w.getPageSize());
            List<WalletCold> userList = walletColdMapper.selectWalletAddressList(w);
            int count = walletColdMapper.getCount(w);
            tableData.put("list", userList);
            tableData.put("count", count);

            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();

        }

        return tableData;
    }

    @Override
    public Map<String, Object> getWalletPlatformData(WalletPlatform w) {
        Map<String, Object> tableData = new HashMap<>();
        try {
            PageHelper.startPage(w.getPageNum(), w.getPageSize());
            List<WalletPlatform> userList = walletPlatformMapper.selectWalletPlatformsList(w);
            int count = walletPlatformMapper.getCount(w);
            tableData.put("list", userList);
            tableData.put("count", count);

            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();

        }

        return tableData;
    }


    @Override
    public Map<String, Object> getWalletHotEntry(UserWithdrawResponeDto dto) {

        Map<String, Object> tableData = new HashMap<>();
        try {
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
            List<UserWithdrawResponeDto> userList = walletHotEntryMapper.selectWalletHotEntry(dto);
            int count = walletHotEntryMapper.getCount(dto);
            for (UserWithdrawResponeDto  wpf:userList) {
                if(wpf.getMsgState() !=null && wpf.getMsgState().equals("1")){
                    wpf.setMsgState("已发送");
                }else{
                    wpf.setMsgState("未发送");
                }
            }
            tableData.put("list", userList);
            tableData.put("count", count);
            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();
        }
        return tableData;
    }


    @Override
    public Map<String, Object> getFinanceListData(WalletFinance wf,Integer pageNum,Integer pageSize) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Map<String, Object> tableData = new HashMap<>();
        try {
            PageHelper.startPage(pageNum, pageSize);
            if(wf.getStartTime() !=null && !wf.getStartTime().equals("")){
                Date date=format.parse(wf.getStartTime());
                wf.setStartTime(String.valueOf(date.getTime()/1000));
            }
            if(wf.getEndTime() !=null && !wf.getEndTime().equals("")){
                Date date=format.parse(wf.getEndTime());
                wf.setEndTime(String.valueOf(date.getTime()/1000));
            }
            List<WalletFinance> financeList = walletFinanceMapper.selectWalletFinanceList(wf);
            int count = walletFinanceMapper.getCount(wf);
            for (WalletFinance  wpf:financeList) {
                if(wpf.getCoinType() !=null && wpf.getCoinType().equals("1")){
                    wpf.setCoinType("USDT");
                }else{
                    wpf.setCoinType("BITCOIN");
                }
            }

            tableData.put("list", financeList);
            tableData.put("count", count);

            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();

        }

        return tableData;
    }


    @Override
    public Map<String, Object> getListpage(Map<String, Object> map) {
        return null;
    }

    @Override
    public List<User> getUser(User user) {
        return null;
    }

    @Override
    public int insertCold(WalletCold w) {
        try {
            return walletColdMapper.insert(w);
        }catch (Exception e){
            log.info("插入地址失败：{}",e.getMessage());
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int insertPlate(WalletPlatform w) {
        try {
            return walletPlatformMapper.insert(w);
        }catch (Exception e){
            log.info("插入地址失败：{}",e.getMessage());
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public WalletBlockHeightResponeDto selectBlockheight(Integer id) {
        WalletBlockHeight wh =new WalletBlockHeight();
        WalletBlockHeightResponeDto wbrd=new WalletBlockHeightResponeDto();
        for(int i=0;i<=id;i++){
            wh=walletBlockHeightMapper.selectByPrimaryKey(i+1);
            if(i==0){
                wbrd.setBlockHeight(wh.getBlockHeigth());
                wbrd.setFlag(wh.getFlag());
            }else if(i==1){
                wbrd.setBlockHeightTx(usdtServie.getBlockCount());
            }else{
                System.out.println(usdtServie.getBlockCount());
            }
        }
      return wbrd;
    }

    @Override
    public FinanceWithdrawResponeDto walletImputationCount(WalletFinance wf) {
        FinanceWithdrawResponeDto fwr=new FinanceWithdrawResponeDto();
        try {
            fwr=getcount(wf);
            fwr.setM(this.getOneWeek(wf.getDataNumber()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return fwr;
    }

    @Override
    public FinanceWithdrawResponeDto walletMentionMoneyCount(WalletFinance wf) {
        FinanceWithdrawResponeDto fwr=new FinanceWithdrawResponeDto();
        wf.setTxType(TX_TYPE_TOW);
        WalletFinance mentionMoney=walletFinanceMapper.walletWithdraw( wf);
        if(mentionMoney !=null){
            fwr.setMentionMoneyTotalAmount(mentionMoney.getAmount());
            fwr.setMentionMoneytalMinerFee(mentionMoney.getMinerFee().toString());
            fwr.setMentionMoneyTotalReseveFee(mentionMoney.getReserveFree());
        }else{
            fwr.setMentionMoneyTotalAmount("0");
            fwr.setMentionMoneytalMinerFee("0");
            fwr.setMentionMoneyTotalReseveFee("0");
        }
        return fwr;
    }

    @Override
    public Boolean updateBlockheight(WalletBlockHeight w) {
        w.setUpdateTime(System.currentTimeMillis());
        try {
            for(int i=1;i<3;i++){
                w.setId(i);
                walletBlockHeightMapper.updateByPrimaryKeySelective(w);
            }
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            log.error("更新区块高度失败：{}",e.getMessage());
            return  false;
        }
    }

    /**
     * 获取时间
     * @param weekday
     * @return
     */
   public Map<String,Object> getOneWeek(int weekday){
       WalletFinance wf=new WalletFinance();
       FinanceWithdrawResponeDto fwr=new FinanceWithdrawResponeDto();
       long current=System.currentTimeMillis();//当前时间毫秒数
       long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
       long twelve=zero+24*60*60*1000-1;//今天23点59分59秒的毫秒数
       Map<String,Object> m=new HashMap<>();
       SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
       ArrayList yData=new ArrayList();
       ArrayList sData=new ArrayList();
       ArrayList zData=new ArrayList();
       ArrayList kData=new ArrayList();
       for (int i=0;i<weekday;i++){
            wf.setStartTime(String.valueOf((zero-24*60*60*1000*(weekday-i-1))/1000));
            wf.setEndTime(String.valueOf((twelve-24*60*60*1000*(weekday-i-1))/1000));
            fwr=getcount(wf);
            if(fwr.getWithdrawTotalAmount()==null){
                yData.add(0);
            }else{
                yData.add(fwr.getWithdrawTotalAmount());
            }
            if(fwr.getImpatationTotalAmount()==null){
                sData.add(0);
            }else{
                sData.add(fwr.getImpatationTotalAmount());
            }

          // zData.add(fwr.getWithdrawTotalAmount());
          // kData.add(fwr.getWithdrawTotalAmount());
       }
        m.put("yData",yData);
        m.put("sData",sData);
        m.put("zData",zData);
        m.put("kData",kData);
        return m;
    }

    private FinanceWithdrawResponeDto getcount(WalletFinance wf){
        FinanceWithdrawResponeDto fwr=new FinanceWithdrawResponeDto();
        try {
            wf.setTxType(TX_TYPE_ONE);
            WalletFinance withdraw=walletFinanceMapper.walletWithdraw(wf);
            if(withdraw !=null){
                fwr.setWithdrawTotalAmount(withdraw.getAmount());
                fwr.setWithdrawTotalMinerFee(withdraw.getMinerFee().toString());
                fwr.setWithdrawTotalReseveFee(withdraw.getReserveFree());
            }
            wf.setTxType(TX_TYPE_SIX);
            WalletFinance imputation=walletFinanceMapper.walletWithdraw(wf);
            if(imputation !=null){
                fwr.setImpatationTotalAmount(imputation.getAmount());
                fwr.setImpatationTotalMinerFee(imputation.getMinerFee().toString());
                fwr.setImpatationTotalReseveFee(imputation.getReserveFree());
            }
            return fwr;
        }catch (Exception e){
            e.printStackTrace();
            return fwr;
        }
    }




}
